package mysql

import (
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"wx/appconfig"
)

var (
	err error
	DB  *gorm.DB
)

func Init() {
	Mysqlconf := appconfig.Appconf.Mysql
	dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local",
		Mysqlconf.User,
		Mysqlconf.Password,
		Mysqlconf.Host,
		Mysqlconf.Port,
		Mysqlconf.Database,
	)
	DB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
	if err != nil {
		panic(err)
	} else {
		fmt.Println("数据库连接成功")
	}
	err = DB.AutoMigrate(&Ass{}, &WeiXin{})
	if err != nil {
		panic(err)
	} else {
		fmt.Println("迁移成功")
	}
}
